package com.hspedu.inputstream;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/8
 * @time 21:39
 */
public class IOTest {
    public static void main(String[] args) {

    }

    @Test
    public void testRead1() throws IOException, InterruptedException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int read;
        while ((read = fileInputStream.read()) != -1) {
            System.out.println(read);
        }
    }

    @Test
    public void testRead2() throws IOException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[3];
        int read = 0;
        while ((read = fileInputStream.read(bytes, 0, 3)) != -1) {
            System.out.println(read);
            System.out.println(new String(bytes, 0, read));
        }
    }

    @Test
    public void testWrite1() throws IOException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\note2.txt");
        OutputStream outputStream = new FileOutputStream(file, true);
        outputStream.write("hello ydl".getBytes());
    }

    @Test
    public void testWrite2() throws IOException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\note2.txt");
        OutputStream outputStream = new FileOutputStream(file, true);
        byte[] bytes = "hello ydl".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            outputStream.write(bytes[i]);
        }

    }

    @Test
    public void testCopyFile() throws IOException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\高山流水2.mp3");

        //1. 创建输入流
        InputStream inputStream = new FileInputStream(file);
        //2. 创建输出流
        OutputStream outputStream = new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\mytemp\\高山流水2.mp3");
        Long start = System.currentTimeMillis();
        int b;
        while ((b = inputStream.read()) != -1) {
            outputStream.write(b);
        }

        Long end = System.currentTimeMillis();
        System.out.println(start - end);
    }

    @Test
    public void testCopyFile2() throws IOException {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\高山流水2.mp3");
        //1. 创建输入流
        InputStream inputStream = new FileInputStream(file);
        //2. 创建输出流
        OutputStream outputStream = new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\mytemp\\高山流水2.mp3");
        Long start = System.currentTimeMillis();
        int len;
        //这个缓冲区就相当于是在内存中的中转
        byte[] buffer = new byte[512];
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        Long end = System.currentTimeMillis();
        System.out.println(start - end);
    }

    @Test
    public void testCopyFile3() {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\高山流水2.mp3");
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            //1. 创建输入流
            inputStream = new FileInputStream(file);
            //2. 创建输出流
            outputStream = new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\mytemp\\高山流水2.mp3");
            Long start = System.currentTimeMillis();
            int len;
            //这个缓冲区就相当于是在内存中的中转
            byte[] buffer = new byte[512];
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            Long end = System.currentTimeMillis();
            System.out.println(start - end);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testCopyFile4() {
        File file = new File("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\高山流水2.mp3");
        try ( //1. 创建输入流
              InputStream inputStream = new FileInputStream(file);
              //2. 创建输出流
              OutputStream outputStream = new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\mytemp\\高山流水2.mp3")) {
            Long start = System.currentTimeMillis();
            int len;
            //这个缓冲区就相当于是在内存中的中转
            byte[] buffer = new byte[512];
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            Long end = System.currentTimeMillis();
            System.out.println(start - end);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
